package com.inossem.warehousestorage_inossem.adapter.refundStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStoragePickingListActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.RefundStoragePickingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.refund_storage.extra.RefundStorageExtra;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStoragePickingListBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class RefundStoragePickingListAdapter extends BaseAdapter {

    private Context mContext;
    private List<RefundStoragePickingListBean.OutPutBodyBean> mPickingRefundStorageList;
    private LayoutInflater inflater;
    private RefundStoragePickingListActivity.RefundStorageManageListener mRefundStorageListener;
    private String mCondition;

    public RefundStoragePickingListAdapter(Context context, List<RefundStoragePickingListBean.OutPutBodyBean> pickingRefundStorageList, String condition, RefundStoragePickingListActivity.RefundStorageManageListener refundStorageListener) {
        mContext = context;
        mPickingRefundStorageList = pickingRefundStorageList;
        mRefundStorageListener = refundStorageListener;
        inflater = LayoutInflater.from(context);
        mCondition = condition;
    }

    @Override
    public int getCount() {
        return mPickingRefundStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mPickingRefundStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_receive_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setText(mCondition);
            condition.setHint(mContext.getResources().getString(R.string.activity_refund_storage_picking_list_please_enter_the_lead_sheet_number));
            condition.addTextChangedListener(new QRCodeEditTextWatcher(condition) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, condition);
                    mRefundStorageListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mRefundStorageListener.onNoDoubleClickListener(condition.getText().toString());
                }
            });
        } else {
            RefundStoragePickingListBean.OutPutBodyBean pickingRefundStorageItem = mPickingRefundStorageList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_refund_storage_picking_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, pickingRefundStorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.pickingNumber = (TextView) view.findViewById(R.id.pickingNumber);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.pickingFactory = (TextView) view.findViewById(R.id.pickingFactory);
        viewHolder.outStorageDate = (TextView) view.findViewById(R.id.outStorageDate);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, RefundStoragePickingListBean.OutPutBodyBean pickingRefundStorageItem) {
        viewHolder.pickingNumber.setText(pickingRefundStorageItem.getMat_req_code());
        viewHolder.outStorageNumber.setText(pickingRefundStorageItem.getStock_output_code());
        viewHolder.pickingFactory.setText(pickingRefundStorageItem.getMat_req_fty_name());
        viewHolder.outStorageDate.setText(pickingRefundStorageItem.getCreate_time());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, RefundStoragePickingOverviewActivity.class);
                intent.putExtra(RefundStorageExtra.OUT_STORAGE_NUMBER, viewHolder.outStorageNumber.getText().toString());
                intent.putExtra(RefundStorageExtra.STOCK_OUTPUT_ID, pickingRefundStorageItem.getStock_output_id());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView pickingNumber, outStorageNumber, pickingFactory, outStorageDate;
    }
}
